﻿// LoginRecorder.cpp : 定义应用程序的入口点。
//

#include "framework.h"
#include "LoginRecorder.h"
#include <Windows.h>
#include <WtsApi32.h>
#include <string.h>
#include <stdio.h>
#include <time.h>

#pragma comment(lib, "wtsapi32.lib")

#define MAX_LOADSTRING 100
#define DefaultFilePath "D:\\loginRecorder.txt"

char* cmdFilePath;

// 全局变量:
HINSTANCE hInst;                                // 当前实例
WCHAR szTitle[MAX_LOADSTRING];                  // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING];            // 主窗口类名

// 此代码模块中包含的函数的前向声明:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
void LogCurrenTime();

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // 初始化全局字符串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_LOGINRECORDER, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    if (!InitInstance (hInstance, nCmdShow))
    {
        MessageBox(NULL, L"注册窗口类失败", L"错误", MB_OK | MB_ICONINFORMATION);
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LOGINRECORDER));

    MSG msg;

    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}


ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = NULL;
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = szWindowClass;
    wcex.hIconSm = NULL;
    return RegisterClassExW(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance;

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_POPUP,
       CW_USEDEFAULT, 0, 0, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      MessageBox(NULL, L"初始窗口失败", L"错误", MB_OK | MB_ICONINFORMATION);
      return FALSE;
   }
   WTSRegisterSessionNotification(hWnd, NOTIFY_FOR_ALL_SESSIONS);

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_WTSSESSION_CHANGE:
        LogCurrenTime();
        break;
    case WM_DESTROY:
        WTSUnRegisterSessionNotification(hWnd);
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

void LogCurrenTime() {
    FILE* file;
    errno_t err = fopen_s(&file, DefaultFilePath, "a+");
    if (err != 0) {
        MessageBox(NULL, L"打开文件失败", L"错误", MB_OK | MB_ICONINFORMATION);
    }
    else {
        time_t curTimeStruct;
        struct tm p;
        char curTime[28];
        memset(curTime, '\0', 20);
        time(&curTimeStruct);
        gmtime_s(&p, &curTimeStruct);
        sprintf_s(curTime, "%4d/%02d/%02d %02d:%02d:%02d\n", (1900+p.tm_year), p.tm_mon, p.tm_mday, 8 + p.tm_hour, p.tm_min, p.tm_sec);
        fwrite(curTime, sizeof(char), 20, file);
        fclose(file);
    }
}
